using UnityEngine;
using UnityEngine.UI;
using TEngine;
using System.Collections.Generic;
using TMPro;
using GameLogic.Localization;
using System;
using static UnityEditor.Progress;
using UnityEngine.UIElements;
using System.Runtime.Remoting.Contexts;
using GameProtocol;
using Google.Protobuf;
using System.IO;

namespace GameLogic
{
    [Window(UILayer.Top)]
    partial class ShipConfigUI
    {
        List<LoopScrollViewItemData> m_WeaponDatas = new List<LoopScrollViewItemData>();
        List<ShipData> m_ShipDatas = new List<ShipData>();
        WeaponInformationPanel m_WeaponInformationPanel;
        WeaponListPanel m_WeaponListPanel;
        ShipListPanel m_ShipListPanel;
        ShipBluemapPanel m_ShipBluemapPanel;

        ShipData m_CurrentShipData;
        int m_SelectedSlotIdx = -1;
        private bool m_IsDialog = false;
        protected override void RegisterEvent()
        {
            base.RegisterEvent();
            m_btnExBG.AddClickEvent(() => {
                m_WeaponInformationPanel.Close();
                m_WeaponListPanel.Close();
            });
            m_btnExBack.AddClickEvent(() => {
                GameModule.UI.CloseUI<ShipConfigUI>();
                if(!m_IsDialog)
                    GameEvent.Send(GameEventType.OnShipConfigBackClicked);
            });
            m_btnExSaveShipConfig.AddClickEvent(() => {
                var byteData = m_CurrentShipData.ToByteArray();
                var fileDir = ShipModule.ShipConfigDir + m_CurrentShipData.CfgID + "/";
                var filePath = fileDir + m_tmpInputSaveConfigName.text +".cfg";
                Directory.CreateDirectory(fileDir);
                if (m_tmpInputSaveConfigName.text != "")
                {
                    UtilsUnity.SaveFile(byteData, filePath);
                    GameModule.UI.ShowUI<FloatingTips>("保存舰船配置成功到：" + filePath);
                }
                else
                {
                    GameModule.UI.ShowUI<FloatingTips>("没有输入配置名称");
                }
            });
        }
        protected override void OnCreate()
        {
            base.OnCreate();
            var root = gameObject.transform.Find("Root");
            m_ShipBluemapPanel = CreateWidgetByPath<ShipBluemapPanel>(root, "Assets/AssetRaw/UI/ShipConfigUI/ShipBluemapPanel.prefab");
            m_WeaponListPanel = CreateWidgetByPath<WeaponListPanel>(root, "Assets/AssetRaw/UI/ShipConfigUI/WeaponListPanel.prefab");
            m_ShipListPanel = CreateWidgetByPath<ShipListPanel>(root, "Assets/AssetRaw/UI/ShipConfigUI/ShipListPanel.prefab");
            m_WeaponInformationPanel = CreateWidgetByPath<WeaponInformationPanel>(root, "Assets/AssetRaw/UI/ShipConfigUI/WeaponInformationPanel.prefab");
            m_ShipListPanel.OnShipSelected = OnShipSelected;
            m_ShipBluemapPanel.OnSlotSelectedCallback = OnSlotClicked;
        }
        protected override void OnRefresh()
        {
            base.OnRefresh();
            m_IsDialog = GetUserData<bool>(0);
            m_WeaponInformationPanel.transform.gameObject.SetActive(false);
            m_WeaponListPanel.OnWeaponSelected = OnWeaponSelected;
            m_WeaponDatas.Clear();
            for (var i = 0; i < GameDataModule.Instance.GetWeaponListLength(); ++i)
            {
                var itemData = new LoopScrollViewItemData(i);
                m_WeaponDatas.Add(itemData);
            }
            m_WeaponDatas.Add(new LoopScrollViewItemData(-1));

            m_ShipDatas.Clear();
            for(var i =0; i< GameDataModule.Instance.GetNumShipData(); ++i)
            {
                var shipData = GameDataModule.Instance.GetShipData(i);
                m_ShipDatas.Add(shipData);
            }

            m_WeaponListPanel.Refresh(m_WeaponDatas);
            m_CurrentShipData = m_ShipDatas[GameDataModule.Instance.GetDriveShipIndex()];
            m_ShipListPanel.SelectedIndex = GameDataModule.Instance.GetDriveShipIndex();
            m_ShipListPanel.Refresh(m_ShipDatas);
            m_ShipBluemapPanel.Refresh(GameDataModule.Instance.GetDriveShipData());
            m_ShipListPanel.Open();
            m_WeaponListPanel.Close();
            m_WeaponInformationPanel.Close();
        }


        #region UI回调
        void OnShipSelected(int idx)
        {
            m_CurrentShipData = m_ShipDatas[idx];
            m_WeaponListPanel.Close();
            m_WeaponInformationPanel.Close();
            //m_ShipListPanel.Close();
            m_ShipBluemapPanel.Refresh(m_CurrentShipData);
        }
        void OnSlotClicked(int idx)
        {
            m_SelectedSlotIdx = idx;
            var slotConfig = m_CurrentShipData.SlotEquipment[idx];
            if (slotConfig.EquipmentID > 0)
            {
                m_WeaponInformationPanel.SwitchWeapon(slotConfig.EquipmentID);
                m_WeaponInformationPanel.Open();
            }
            else
            {
                m_WeaponInformationPanel.Close();
            }

            int selecteIndex = -1;
            m_WeaponListPanel.Open();
            for (int i =0; i<m_WeaponDatas.Count; ++i)
            {
                var weaponID = GameDataModule.Instance.GetWeaponID(i);
                if(slotConfig.EquipmentID == weaponID)
                {
                    selecteIndex = i;
                    break;
                }
            }
            m_WeaponListPanel.Selecte(selecteIndex);
        }
        void OnWeaponSelected(int idx)
        {
            var data = m_WeaponDatas[idx];
            var weaponID = (data.ID < 0 ? -1 : GameDataModule.Instance.GetWeaponID(data.ID));
            m_WeaponInformationPanel.transform.gameObject.SetActive(true);
            m_WeaponInformationPanel.SwitchWeapon(weaponID);
            m_ShipBluemapPanel.OnWeaponChanged(weaponID);

            var slotConfig = m_CurrentShipData.SlotEquipment[m_SelectedSlotIdx];
            GameDataModule.Instance.AddItem(slotConfig.EquipmentID, 1);
            GameDataModule.Instance.RemoveItem(weaponID, 1);
            slotConfig.EquipmentID = weaponID;
        }
        #endregion
    }
}